const {
    Msg,
    Code,
    Constant
} = require('./consts');
const {
    addSonVideoReward,
    addPersonalVideoReward,
    getParentUid,
    addGrandSonVideoReward,
    addMoneyVideoReward,
    addUserTotalVideo,
    addUserValidSon,
    addIncreaseSonReward
} = require('./api');
const {
    markUserVideoCache,
    videoMarkExistsCache
} = require('./cache');
const {
    addDailyVideoNumber
} = require('./dao');

var pushUitls = require('../../../util/pushUtils');

/**
 * 用户看视频
 * @param session
 * @param uid
 * @returns {Promise<void>}
 */
exports.watchVideo = async function ({session, uid}) {
    //判断用户是否存在
    let userInfo = await this.app.userRemote(uid, 'get', [uid, 'user']);
    if (!userInfo) {
        throw new Error(Msg.USER_NOT_FOUND);
    }
    //看视频次数已经达到上限了
    if (userInfo.video_num >= Constant.VIDEO_LIMIT_NUM) {
        throw new Error(Msg.VIDEO_LIMIT);
    }
    //达到了个人每日看视频次数要求，更新个人奖励状态
    if (userInfo.video_num + 1 == Constant.PERSON_VIDEO_NUM) {
        //更新自己的日常视频任务
        await addPersonalVideoReward.call(this, session, userInfo.uid);
        //更新活动奖励0.3元的任务
        await addMoneyVideoReward.call(this, session, userInfo.uid);
        //看够30次视频变成了有效用户
        userInfo.is_effect_user = true;
        if (userInfo.parent_id != "0") {
            //师傅增加邀请奖励
            await addIncreaseSonReward.call(this, session, userInfo.parent_id, userInfo.uid);
            //用户变为有效徒弟
            await addUserValidSon.call(this, session, userInfo.parent_id, userInfo.uid);
        }
    }
    //达到了视频上限，更新师傅或师爷的日常任务奖励状态
    if (userInfo.video_num + 1 == Constant.VIDEO_LIMIT_NUM) {
        //有师傅
        if (userInfo.parent_id != '0') {
            //增加师傅的日常任务奖励
            await addSonVideoReward.call(this, session, userInfo.parent_id);
            //获取师爷的用户ID
            let parentParentUid = await getParentUid.call(this, session, userInfo.parent_id);
            if (parentParentUid != "0") {
                //给师爷增加日常任务奖励
                await addGrandSonVideoReward.call(this, session, parentParentUid);
            }
        }
    }
    //增加个人视频观看次数
    userInfo.video_num = Number(userInfo.video_num) + 1 > Constant.VIDEO_LIMIT_NUM ? Constant.VIDEO_LIMIT_NUM : Number(userInfo.video_num) + 1;
    userInfo.total_video = Number(userInfo.video_num) + 1;
    //更新内存中的用户视频次数
    await this.app.userRemote(uid, "set", [uid, "user.video_num", userInfo.video_num]);
    //总视频次数
    await addUserTotalVideo.call(this, session, uid);
    //更新每日视频数量到MySQL，用于运营后台统计
    await addDailyVideoNumber.call(this, uid);
    //更新每日视频数量到Redis，用于每日重置视频数量
    await markUserVideoCache.call(this, uid);
    //有效用户
    await this.app.userRemote(uid, "set", [uid, "user.is_effect_user", userInfo.is_effect_user]);
    //推送消息给客户端
    pushUitls.onVideoAdd(this.app, {increase: 1, current: userInfo.video_num}, uid, session.frontendId);
}

/**
 * 判断用户是否有今日观看视频的标识
 * @param session
 * @param uid
 * @returns {Promise<any>}
 */
exports.videoMarkExistsAction = async function (uid) {
    return await videoMarkExistsCache.call(this, uid);
}